package com.police.db;

/**
 * Created by LEO on 13-6-6.
 */

import java.util.ArrayList;
import java.util.List;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;

public class UserManager {
    private DBHelper helper;
    private SQLiteDatabase db;

//    public UserManager(Context context) {
//        helper = new DBHelper(context);
//        //因为getWritableDatabase内部调用了mContext.openOrCreateDatabase(mName, 0, mFactory);
//        //所以要确保context已初始化,我们可以把实例化DBManager的步骤放在Activity的onCreate里
//        db = helper.getWritableDatabase();
//    }
    
    public UserManager(DBHelper helper) {
    	this.helper = helper;
        //因为getWritableDatabase内部调用了mContext.openOrCreateDatabase(mName, 0, mFactory);
        //所以要确保context已初始化,我们可以把实例化DBManager的步骤放在Activity的onCreate里
        db = helper.getWritableDatabase();
    }

    /**
     * add Users
     *
     * @param Users
     */
    public void add(List<User> Users) {
        db.beginTransaction();    //开始事务
        try {
            for (User User : Users) {
//                db.execSQL("INSERT INTO User VALUES(?, ?, ?)", new Object[]{User.name, User.password,User.isAdmin});
//                db.execSQL("INSERT INTO User VALUES(null, ?, ?)", new Object[]{User.name, User.password, User.isAdmin});
                
                ContentValues cv=new ContentValues();    //实例化ContentValues对象
                cv.put("name", User.name);                  //插入字段值
                cv.put("password", User.password); 
                cv.put("isAdmin", User.isAdmin);//插入字段值
                db.insert("User", null, cv);         //执行insert()方法
                
                
            }
            db.setTransactionSuccessful();    //设置事务成功完成
        } finally {
            db.endTransaction();    //结束事务
        }
    }

    
    public void deleteUsers(List<String> userNameList) {
    	
    	db.beginTransaction();    //开始事务
        try {
            for (String userName : userNameList) {
//                db.delete("User", "name" + "=" + userName, null );
                db.delete("User", "name=?", new String[]{userName});  
            }
            db.setTransactionSuccessful();    //设置事务成功完成
        } finally {
            db.endTransaction();    //结束事务
        }
    	
    }
    
    
    /**
     * 修改用户密码
     * @param newPassWord
     */
    public void changePassWord(String userName,String psd) {
        db.beginTransaction();    //开始事务
        try {
//            for (User User : Users) {
//                db.execSQL("INSERT INTO User VALUES(?, ?, ?)", new Object[]{User.name, User.password,User.isAdmin});
//                db.execSQL("INSERT INTO User VALUES(null, ?, ?)", new Object[]{User.name, User.password, User.isAdmin});
                
//                ContentValues cv=new ContentValues();    //实例化ContentValues对象
//                cv.put("name", User.name);                  //插入字段值
//                cv.put("password", User.password); 
//                cv.put("isAdmin", User.isAdmin);//插入字段值
//                db.insert("User", null, cv);         //执行insert()方法
                
                ContentValues cv = new ContentValues();   
                cv.put("password", psd); 
//            	db.update("User", cv, "name" + "=" + userName, null);
//            	db.update("User", cv, "_id=?", new int[]{0}); 
            	db.update("User", cv, "name=?", new String[]{userName}); 
//            }
            
            db.setTransactionSuccessful();    //设置事务成功完成
        } finally {
            db.endTransaction();    //结束事务
        }
    }
    
    /**
     * query all Users, return list
     *
     * @return List<User>
     */
    public List<User> query() {
        ArrayList<User> Users = new ArrayList<User>();
        Cursor c = queryTheCursor();
        while (c.moveToNext()) {
            User User = new User();
            User._id = c.getInt(c.getColumnIndex("_id"));
            User.name = c.getString(c.getColumnIndex("name"));
            User.password = c.getString(c.getColumnIndex("password"));
            User.isAdmin = c.getInt(c.getColumnIndex("isAdmin"));
            Users.add(User);
        }
        c.close();
        return Users;
    }

    /**
     * 判断登录的用户是否存在
     * @return
     */
    public boolean queryByLogin(String userName,String passWord) {
    	Cursor c = queryTheCursor();
    	while (c.moveToNext()) {
            User User = new User();
            User._id = c.getInt(c.getColumnIndex("_id"));
            User.name = c.getString(c.getColumnIndex("name"));
            User.password = c.getString(c.getColumnIndex("password"));
            User.isAdmin = c.getInt(c.getColumnIndex("isAdmin"));
            if (userName.equals(User.name) && passWord.equals(User.password)) {
            	return true;
            }
        }
        c.close();
		return false;
    	
    }
    
    
    /**
     * 判断将要添加的用户是否已经存在
     * @return
     */
    public boolean queryByLogin(String userName) {
    	Cursor c = queryTheCursor();
    	while (c.moveToNext()) {
            User User = new User();
            User._id = c.getInt(c.getColumnIndex("_id"));
            User.name = c.getString(c.getColumnIndex("name"));
            User.password = c.getString(c.getColumnIndex("password"));
            User.isAdmin = c.getInt(c.getColumnIndex("isAdmin"));
            if (userName.equals(User.name)) {
            	return true;
            }
        }
        c.close();
		return false;
    	
    }
    
    /**
     * query all Users, return cursor
     *
     * @return Cursor
     */
    public Cursor queryTheCursor() {
        Cursor c = db.rawQuery("SELECT * FROM User", null);
        return c;
    }

    /**
     * close database
     */
    public void closeDB() {
        db.close();
    }
}

